package it.ipermemory;

import it.ipermemory.graphic.CustomPopup;
import it.ipermemory.logic.Thing;
import it.ipermemory.logic.Game;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

/**
 * Activity that represents the logic of the normal game.
 * 
 * @author Stefano Tonello (stefano.tonello8821@gmail.com)
 * @version 1.0
 */
public class ImageActivity extends Activity {

	/**
	 * representative object id
	 */
	int thing_id;

	/**
	 * represents the image to display on the screen
	 */
	protected Drawable drawable = null;

	/**
	 * A method to create the activity
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image);

		ImageView iv = (ImageView) findViewById(R.id.image_thing);

		thing_id = getIntent().getExtras().getInt(Game.GAME_THING_CARD);

		Thing thing = Game.parserXML.getThingById(thing_id);

		if (thing == null) {
			mexError();
		} else {
			Resources res = getResources();
			int resID = res.getIdentifier(thing.getImage_compress(),
					"drawable", getPackageName());
			drawable = res.getDrawable(resID);
		}

		iv.setImageDrawable(drawable);
	}

	/**
	 * displays a pop-up error
	 */
	private void mexError() {
		CustomPopup cp = new CustomPopup(ImageActivity.this, getResources()
				.getString(R.string.error_media_load)) {

			@Override
			public void onClick(View v) {
				this.dismiss();
				finish();
			}
		};
		cp.getNoButton().setVisibility(View.GONE);
		cp.show();
	}

}
